package com.book.controller;

import static com.github.drinkjava2.jsqlbox.DB.pagin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import com.book.entity.BookChapter;
import com.book.entity.BookInfo;
import com.book.model.BookInfoModel;
import com.book.service.itf.IndexServiceItf;


/**
*
*     小说前端页控制类
* 
*/

@Controller
public class IndexController {

	private IndexServiceItf indexService;
	public IndexController() {
		super();
	}
	/*
	 * 
	 * 前端显示，首页小说数据查询
	 * 
	 * 
	 * 
	 * 
	 */
	
	
	@GetMapping ({ "" , "/" })
	public ModelAndView index() {
	
		ModelAndView mv = new ModelAndView();
		
		mv.addObject("currentClass", 1);
		
		
		 //1.显示最先更新的四本小说
		List<BookInfo> bookInfos = new BookInfo().findBySQL(" select * from book_info order by update_time desc",pagin(1, 4));
		mv.addObject("newBookInfos", bookInfos);
		
		List<BookInfo> dianZanInfos  = new BookInfo().findBySQL(" select * from book_info order by dian_zan desc",pagin(1,8));
		mv.addObject("dianZanInfos", dianZanInfos);
		
		mv.setViewName("index");
		
		return mv;
		
	}
	
	
	
	/*
	 * 
	 * 小说章节列表及 基本信息 查询
	 */


	
	public String book(@PathVariable Integer id,Model m) {
		
		BookInfoModel mo =indexService.getBookInfoFromID(id);
		
		m.addAttribute("eo", mo.getBook());
		m.addAttribute("chapterList", mo.getChapterlist());
		return "book"; 
	}
	 

	/*
	 * 小说章节内容
	 */
	@GetMapping("/chapter/{id}")
public String chapter(@PathVariable Integer id,Model m) {
		
		m.addAttribute("currentClass",1);
		
		BookChapter eo = indexService.getChapterFormID(id);
		
		m.addAttribute("eo", eo);
		
		return "chapter"; 
	}
	
	

/*
 * 
 * 热门小说章节内容
 * 
 * 
 * 
 */
	@GetMapping("/remen")
public String listReMen(HttpServletRequest req, HttpServletResponse resp) {
	req.setAttribute("currentClass", 2);
	
	List<BookInfo> remenList = indexService.getRemenList();
	
	req.setAttribute("dataList", remenList);
	
	return "book_list"; 
}
	
	
	
	
	
}
